package service.impl;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import common.impl.MicroBlog;
import common.wx.WxAuth;
import common.wx.WxConfig;

import service.interf.IKefu;

public class KefuImpl extends BaseDataServiceImpl implements IKefu {

	private static final String url_send = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";

	public String sendText(String openId, String content) throws Exception {
		System.out.println("openId = " + openId);
		System.out.println("content = " + content);
		if(StringUtils.isEmpty(openId))
			return null;
		if(StringUtils.isEmpty(content))
			return null;
		String token_url = WxConfig.url_gettoken;
		token_url = token_url.replace("APPID", WxAuth.APPID);
		token_url = token_url.replace("APPSECRET", WxAuth.APPSECRET);
		String resp = MicroBlog.post(token_url, null);
		JSONObject joo = JSONObject.fromObject(resp);
		String accessToken = joo.getString("access_token");
		if (StringUtils.isEmpty(accessToken))
			accessToken = null;
		if (null == accessToken)
			return "0";
		String url = url_send.replace("ACCESS_TOKEN", accessToken);
		JSONObject jo = new JSONObject();
		jo.put("touser", openId);
		jo.put("msgtype", "text");
		JSONObject jo2 = new JSONObject();
		jo2.put("content", content);
		jo.put("text", jo2);
		String r = MicroBlog.post(url, jo.toString(), null);
		System.out.println("r = " + r);
		return "1";
	}

}
